package com.ant.androiddemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ant.androiddemo.R;
import com.ant.androiddemo.bean.ItemBean;

import java.util.List;

public class MyAdapter extends BaseAdapter {

    private List<ItemBean> mBeanList;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    public MyAdapter(Context context, List<ItemBean> beanList) {
        this.mContext = context;
        this.mBeanList = beanList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public int getCount() {
        return mBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return mBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        // 创建视图控件
        ViewHolder viewHolder = new ViewHolder();
        if (view == null) {
            view = mLayoutInflater.inflate(R.layout.list_item_layout, viewGroup, false);
            ImageView imageView = view.findViewById(R.id.iv_img);
            TextView tvTitle = view.findViewById(R.id.tv_title);
            TextView tvContent = view.findViewById(R.id.tv_content);
            viewHolder.imageView = imageView;
            viewHolder.tvTitle = tvTitle;
            viewHolder.tvContent = tvContent;
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        // 为控件填充数据
        ItemBean itemBean = mBeanList.get(i);
        viewHolder.imageView.setImageResource(itemBean.getImgResId());
        viewHolder.tvTitle.setText(itemBean.getTitle());
        viewHolder.tvContent.setText(itemBean.getContent());
        return view;
    }



    class ViewHolder {
        ImageView imageView;
        TextView tvTitle;
        TextView tvContent;
    }
}
